<?php

/*
Copyright 2009-2011 Sam Weiss
All Rights Reserved.

This file is part of Spark/Plug.

Spark/Plug is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('spark/plug'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

// -----------------------------------------------------------------------------

class _SparkMailer extends SparkPlug
{
	private $_mailer;

	// --------------------------------------------------------------------------

	public function __construct($params = NULL)
	{
		parent::__construct($params);

		$hostName = isset($params['host']) ? $params['host'] : $this->config->get('app_host', 'localhost.localdomain');
		$transport = isset($params['mail_transport']) ? $params['mail_transport'] : $this->config->get('mail_transport', 'sendmail');
		$smtpHost = isset($params['smtpHost']) ? $params['smtp_host'] : $this->config->get('smtp_host', NULL);
		$charset = isset($params['charset']) ? $params['charset'] : $this->config->get('charset', 'UTF-8');

		$myInfo = $this->factory->getPlug('SparkMailer');
		$plugLibDir = dirname($myInfo['file']) . '/lib/';
		
		require($plugLibDir . 'class.phpmailer.php');
		$this->_mailer = new PHPMailer();

		$this->_mailer->SetLanguage($this->config->get('language', 'en_US'), $plugLibDir . 'language/');
		if ($this->_mailer->ErrorInfo)
		{
			throw new SparkException("Mailer error: {$this->_mailer->ErrorInfo}");
		}
		$this->hostName($hostName)->transport($transport, $smtpHost)->charset($charset);
	}

	// --------------------------------------------------------------------------

	public function hostName($hostName)
	{
		$this->_mailer->Hostname = $hostName;
		return $this;
	}

	// --------------------------------------------------------------------------

	public function transport($transport, $smtpHost = NULL)
	{
		switch ($transport)
		{
			case 'php':
				$this->_mailer->IsMail();
				break;
			case 'sendmail':
				$this->_mailer->IsSendmail();
				break;
			case 'smtp':
				$this->_mailer->Host = $smtpHost;
				$this->_mailer->IsSMTP();
				break;
			default:
				throw new SparkException("Invalid mail transport: {$transport}");
		}
		
		return $this;
	}

	// --------------------------------------------------------------------------

	public function charset($charset)
	{
		$this->_mailer->CharSet = $charset;
		return $this;
	}

	// --------------------------------------------------------------------------

	public function isHTML($isHTML)
	{
		$this->_mailer->IsHTML($isHTML);
		return $this;
	}

	// --------------------------------------------------------------------------

	public function sender($param = NULL)
	{
		if (!empty($param))
		{
			$this->_mailer->Sender = $param;
		}
		return $this;
	}

	// --------------------------------------------------------------------------

	public function from($param = NULL)
	{
		if (!empty($param))
		{
			$this->_mailer->From = $param;
		}
		return $this;
	}

	// --------------------------------------------------------------------------

	public function fromName($param = NULL)
	{
		if (!empty($param))
		{
			$this->_mailer->FromName = $param;
		}
		return $this;
	}

	// --------------------------------------------------------------------------

	public function subject($param = NULL)
	{
		if (!empty($param))
		{
			$this->_mailer->Subject = $param;
		}
		return $this;
	}

	// --------------------------------------------------------------------------

	public function body($param = NULL)
	{
		if (!empty($param))
		{
			$this->_mailer->Body = $param;
		}
		return $this;
	}

	// --------------------------------------------------------------------------

	public function altBody($param = NULL)
	{
		if (!empty($param))
		{
			$this->_mailer->AltBody = $param;
		}
		return $this;
	}

	// --------------------------------------------------------------------------

	public function addAddress($address, $name = '')
	{
		$this->_mailer->AddAddress($address, $name);
		return $this;
	}

	// --------------------------------------------------------------------------

	public function addEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream')
	{
		$this->_mailer->AddEmbeddedImage($path, $cid, $name, $encoding, $type);
		return $this;
	}

	// --------------------------------------------------------------------------

	public function addAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream')
	{
		$this->_mailer->AddAttachment($path, $name, $encoding, $type);
		return $this;
	}

	// --------------------------------------------------------------------------

	public function send()
	{
		if (!$this->_mailer->Send())
		{
			throw new SparkException("Could not send mail: {$this->_mailer->ErrorInfo}");
		}
	}

	// --------------------------------------------------------------------------
}
